// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.textures;

import java.util.ArrayList;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.PerformanceSettings;
import zombie.core.SpriteRenderer;
import zombie.core.utils.ImageUtils;
import zombie.debug.DebugLog;
import zombie.iso.IsoCamera;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoUtils;
import zombie.iso.PlayerCamera;
import zombie.iso.sprite.IsoCursor;
import zombie.network.GameServer;
import zombie.network.ServerGUI;
import zombie.util.Type;

public final class MultiTextureFBO2 {
    private final float[] zoomLevelsDefault = new float[]{2.5F, 2.25F, 2.0F, 1.75F, 1.5F, 1.25F, 1.0F, 0.75F, 0.5F};
    private float[] zoomLevels;
    public TextureFBO Current;
    public volatile TextureFBO FBOrendered = null;
    public final float[] zoom = new float[4];
    public final float[] targetZoom = new float[4];
    public final float[] startZoom = new float[4];
    private float zoomedInLevel;
    private float zoomedOutLevel;
    public final boolean[] bAutoZoom = new boolean[4];
    public boolean bZoomEnabled = true;

    public MultiTextureFBO2() {
        for (int _int = 0; _int < 4; _int++) {
            this.zoom[_int] = this.targetZoom[_int] = this.startZoom[_int] = 1.0F;
        }
    }

    public int getWidth(int playerIndex) {
        return (int)((float)IsoCamera.getScreenWidth(playerIndex) * this.zoom[playerIndex] * ((float)Core.TileScale / 2.0F));
    }

    public int getHeight(int playerIndex) {
        return (int)((float)IsoCamera.getScreenHeight(playerIndex) * this.zoom[playerIndex] * ((float)Core.TileScale / 2.0F));
    }

    public void setTargetZoom(int playerIndex, float target) {
        if (this.targetZoom[playerIndex] != target) {
            this.targetZoom[playerIndex] = target;
            this.startZoom[playerIndex] = this.zoom[playerIndex];
        }
    }

    public ArrayList<Integer> getDefaultZoomLevels() {
        ArrayList arrayList = new ArrayList();
        float[] _float = this.zoomLevelsDefault;

        for (int _int = 0; _int < _float.length; _int++) {
            arrayList.add(Math.round(_float[_int] * 100.0F));
        }

        return arrayList;
    }

    public void setZoomLevelsFromOption(String levels) {
        this.zoomLevels = this.zoomLevelsDefault;
        if (levels != null && !levels.isEmpty()) {
            String[] string0 = levels.split(";");
            if (string0.length != 0) {
                ArrayList arrayList = new ArrayList();

                for (String string1 : string0) {
                    if (!string1.isEmpty()) {
                        try {
                            int int0 = Integer.parseInt(string1);

                            for (float _float : this.zoomLevels) {
                                if (Math.round(_float * 100.0F) == int0) {
                                    if (!arrayList.contains(int0)) {
                                        arrayList.add(int0);
                                    }
                                    break;
                                }
                            }
                        } catch (NumberFormatException numberFormatException) {
                        }
                    }
                }

                if (!arrayList.contains(100)) {
                    arrayList.add(100);
                }

                arrayList.sort((integer0, integer1) -> integer1 - integer0);
                this.zoomLevels = new float[arrayList.size()];

                for (int int1 = 0; int1 < arrayList.size(); int1++) {
                    int int2 = IsoPlayer.getPlayerIndex();
                    if (Core.getInstance().getOffscreenHeight(int2) > 1440) {
                        this.zoomLevels[int1] = (float)((Integer)arrayList.get(int1)).intValue() / 100.0F - 0.25F;
                    } else {
                        this.zoomLevels[int1] = (float)((Integer)arrayList.get(int1)).intValue() / 100.0F;
                    }
                }
            }
        }
    }

    public void destroy() {
        if (this.Current != null) {
            this.Current.destroy();
            this.Current = null;
            this.FBOrendered = null;

            for (int _int = 0; _int < 4; _int++) {
                this.zoom[_int] = this.targetZoom[_int] = 1.0F;
            }
        }
    }

    public void create(int xres, int yres) throws Exception {
        if (this.bZoomEnabled) {
            if (this.zoomLevels == null) {
                this.zoomLevels = this.zoomLevelsDefault;
            }

            this.zoomedInLevel = this.zoomLevels[this.zoomLevels.length - 1];
            this.zoomedOutLevel = this.zoomLevels[0];
            int int0 = ImageUtils.getNextPowerOfTwoHW(xres);
            int int1 = ImageUtils.getNextPowerOfTwoHW(yres);
            this.Current = this.createTexture(int0, int1, false);
        }
    }

    public void update() {
        int _int = IsoPlayer.getPlayerIndex();
        if (!this.bZoomEnabled) {
            this.zoom[_int] = this.targetZoom[_int] = 1.0F;
        }

        if (this.bAutoZoom[_int] && IsoCamera.CamCharacter != null && this.bZoomEnabled) {
            float float0 = IsoUtils.DistanceTo(IsoCamera.getRightClickOffX(), IsoCamera.getRightClickOffY(), 0.0F, 0.0F);
            float float1 = float0 / 300.0F;
            if (float1 > 1.0F) {
                float1 = 1.0F;
            }

            float float2 = this.shouldAutoZoomIn() ? this.zoomedInLevel : this.zoomedOutLevel;
            float2 += float1;
            if (float2 > this.zoomLevels[0]) {
                float2 = this.zoomLevels[0];
            }

            if (IsoCamera.CamCharacter.getVehicle() != null) {
                float2 = this.getMaxZoom();
            }

            this.setTargetZoom(_int, float2);
        }

        float float3 = 0.004F * zombie.GameTime.instance.getMultiplier() / zombie.GameTime.instance.getTrueMultiplier() * (Core.TileScale == 2 ? 1.5F : 1.5F);
        if (!this.bAutoZoom[_int]) {
            float3 *= 5.0F;
        } else if (this.targetZoom[_int] > this.zoom[_int]) {
            float3 *= 1.0F;
        }

        if (this.targetZoom[_int] > this.zoom[_int]) {
            this.zoom[_int] = this.zoom[_int] + float3;
            IsoPlayer.players[_int].dirtyRecalcGridStackTime = 2.0F;
            if (this.zoom[_int] > this.targetZoom[_int] || Math.abs(this.zoom[_int] - this.targetZoom[_int]) < 0.001F) {
                this.zoom[_int] = this.targetZoom[_int];
            }
        }

        if (this.targetZoom[_int] < this.zoom[_int]) {
            this.zoom[_int] = this.zoom[_int] - float3;
            IsoPlayer.players[_int].dirtyRecalcGridStackTime = 2.0F;
            if (this.zoom[_int] < this.targetZoom[_int] || Math.abs(this.zoom[_int] - this.targetZoom[_int]) < 0.001F) {
                this.zoom[_int] = this.targetZoom[_int];
            }
        }

        this.setCameraToCentre();
    }

    private boolean shouldAutoZoomIn() {
        if (IsoCamera.CamCharacter == null) {
            return false;
        } else {
            IsoGridSquare isoGridSquare = IsoCamera.CamCharacter.getCurrentSquare();
            if (isoGridSquare != null && !isoGridSquare.isOutside()) {
                return true;
            } else {
                IsoPlayer isoPlayer = Type.tryCastTo(IsoCamera.CamCharacter, IsoPlayer.class);
                if (isoPlayer == null) {
                    return false;
                } else if (isoPlayer.isRunning() || isoPlayer.isSprinting()) {
                    return false;
                } else {
                    return isoPlayer.closestZombie < 6.0F && isoPlayer.isTargetedByZombie()
                        ? true
                        : isoPlayer.lastTargeted < (float)(PerformanceSettings.getLockFPS() * 4);
                }
            }
        }
    }

    private void setCameraToCentre() {
        PlayerCamera playerCamera = IsoCamera.cameras[IsoPlayer.getPlayerIndex()];
        playerCamera.center();
    }

    private TextureFBO createTexture(int int0, int int1, boolean _boolean) {
        if (_boolean) {
            Texture texture0 = new Texture(int0, int1, 16);
            TextureFBO textureFBO = new TextureFBO(texture0);
            textureFBO.destroy();
            return null;
        } else {
            Texture texture1 = new Texture(int0, int1, 19);
            return new TextureFBO(texture1);
        }
    }

    public void render() {
        if (this.Current != null) {
            int int0 = 0;

            for (int int1 = 3; int1 >= 0; int1--) {
                if (IsoPlayer.players[int1] != null) {
                    int0 = int1 > 1 ? 3 : int1;
                    break;
                }
            }

            int0 = Math.max(int0, IsoPlayer.numPlayers - 1);

            for (int int2 = 0; int2 <= int0; int2++) {
                if (Core.getInstance().RenderShader != null) {
                    zombie.IndieGL.StartShader(Core.getInstance().RenderShader, int2);
                }

                int int3 = IsoCamera.getScreenLeft(int2);
                int int4 = IsoCamera.getScreenTop(int2);
                int int5 = IsoCamera.getScreenWidth(int2);
                int int6 = IsoCamera.getScreenHeight(int2);
                if (IsoPlayer.players[int2] != null || GameServer.bServer && ServerGUI.isCreated()) {
                    ((Texture)this.Current.getTexture())
                        .rendershader2((float)int3, (float)int4, (float)int5, (float)int6, int3, int4, int5, int6, 1.0F, 1.0F, 1.0F, 1.0F);
                } else {
                    SpriteRenderer.instance.renderi(null, int3, int4, int5, int6, 0.0F, 0.0F, 0.0F, 1.0F, null);
                }
            }

            if (Core.getInstance().RenderShader != null) {
                zombie.IndieGL.EndShader();
            }

            IsoCursor.getInstance().render(0);
        }
    }

    public TextureFBO getCurrent(int nPlayer) {
        return this.Current;
    }

    public Texture getTexture(int nPlayer) {
        return (Texture)this.Current.getTexture();
    }

    public void doZoomScroll(int playerIndex, int del) {
        this.targetZoom[playerIndex] = this.getNextZoom(playerIndex, del);
    }

    public float getNextZoom(int playerIndex, int del) {
        if (this.bZoomEnabled && this.zoomLevels != null) {
            if (del > 0) {
                for (int int0 = this.zoomLevels.length - 1; int0 > 0; int0--) {
                    if (this.targetZoom[playerIndex] == this.zoomLevels[int0]) {
                        return this.zoomLevels[int0 - 1];
                    }
                }
            } else if (del < 0) {
                for (int int1 = 0; int1 < this.zoomLevels.length - 1; int1++) {
                    if (this.targetZoom[playerIndex] == this.zoomLevels[int1]) {
                        return this.zoomLevels[int1 + 1];
                    }
                }
            }

            return this.targetZoom[playerIndex];
        } else {
            return 1.0F;
        }
    }

    public float getMinZoom() {
        return this.bZoomEnabled && this.zoomLevels != null && this.zoomLevels.length != 0 ? this.zoomLevels[this.zoomLevels.length - 1] : 1.0F;
    }

    public float getMaxZoom() {
        return this.bZoomEnabled && this.zoomLevels != null && this.zoomLevels.length != 0 ? this.zoomLevels[0] : 1.0F;
    }

    public boolean test() {
        try {
            this.createTexture(16, 16, true);
            return true;
        } catch (Exception exception) {
            DebugLog.General.error("Failed to create Test FBO");
            exception.printStackTrace();
            Core.SafeMode = true;
            return false;
        }
    }
}
